// search code
var areaarr = new Array();

var keyword = '';
var count = 0;
var currentNum = 0;
var textVal = '';
var hiddenVal = '';
var divIdValue = 0;
var mydivIdValue=0;
var divname = '';
var objtxtname = '';
var objvalname = '';
var txtLen = 1; //
var key;
var tid = null;
var index_Val = 0;
var url;
var stype;
var portType = 0;
var isShowDivVal = false;
var placeLang;
var sign;


function trim(str) {
	return str.replace(/\s+$|^\s+/g, "");
}
$(function(){
	var keys = "os.work.airPort,os.work.seaPort,ps.my.placeName";
	getTextByKeys(keys,function(lang){
		placeLang = lang;
	});
});

function seachDataByKeyword(urlval, keywordVal, divNameVal, objtxtnameVal, objvalnameVal,flag ,pageNum, type, pstype) {
	stype = pstype;
	document.onkeydown = keyDown;
	divname = divNameVal;
	url = urlval;
	var i = 0;
	var divArr = window.document.getElementsByTagName("div");	
	for(i=0;i<divArr.length;i++){
		if (divArr[i].getAttribute("name") != divname && divArr[i].getAttribute("name") !=null && divArr[i].getAttribute("name")+"" != "NaN" && divArr[i].getAttribute("name") .indexOf('search_suggest') != -1 ){					
			divArr[i].innerHTML = '';
			textVal = '';
			divArr[i].style.display = 'none';
		}
	}
	
	if( trim( keywordVal ).length==0){
		document.getElementById( objvalnameVal ).value='';
		document.getElementById( objtxtnameVal ).value='';
		document.getElementById(divname).innerHTML = '';
		document.getElementById(divname).style.display = 'none';
		return;
	}
	if (keywordVal.length < txtLen && keywordVal!="*") {
		if (keywordVal.length == 0) {
			document.getElementById(divname).innerHTML = '';
			document.getElementById(divname).style.display = 'none';
			areaarr.length = 0;
		}
		return;
	}
	
	if ( key == 13 || key == 37 || key == 39  || key == 38 || key == 40) {
		return;
	}
	keyword = keywordVal;
	currentNum = pageNum;
	objtxtname = objtxtnameVal;
	objvalname = objvalnameVal;
	divIdValue = 0;
	if(type)portType = type;
	sendAndRepectData();
}

function sendAndRepectData(){ 
	if (tid != null) {
		clearTimeout(tid);
	}
	
	tid = setTimeout(function() {				
				var dataString = "keyword=" + keyword + "&pageNumber=" + currentNum+ "&portType="+portType;
				index_Val = currentNum;	

				$.ajax({
							type : "POST",
							url : url,
							data : dataString,
							dataType : "xml",
							cache : false,
							success : function(XMLHttpRequest) {
								var name = "";
								var id = "";
								areaarr.length = 0;
								count = $(XMLHttpRequest).find('count').attr("count");								
								$(XMLHttpRequest).find('searchObj').each(function() {
									name = $(this).attr("value");
									id = $(this).attr("key");
									areaarr[areaarr.length] = new Array(name, id);
								});			
								sign=$(XMLHttpRequest).find('signNode').attr("sign");
								writeContent(currentNum);
							},
							error: function (XMLHttpRequest){
								
							}
						});
						
			}, 200);

	stopFocus();
}

function stopFocus(  ){
	var txtnameVal = document.getElementById(objtxtname);

	if (navigator.appName.indexOf("Explorer") > -1) {
		var r = txtnameVal.createTextRange();
		r.moveStart('character', txtnameVal.value.length);
		r.collapse(true);
		r.select();
	} else {
		txtnameVal.setSelectionRange(txtnameVal.value.length, txtnameVal.value.length);
		txtnameVal.focus();
	}
}

function suggestOver(div_value) {
	div_value.className = 'suggest_link_over';
}

function suggestOut(div_value) {
	div_value.className = 'suggest_link';
}

function setSearch(hiddenValue, txtValue) {
	textVal = txtValue;
	var ss=textVal.split("|");
	hiddenVal = hiddenValue;
	
	document.getElementById(objtxtname).value = ss[0];
	
	document.getElementById(objvalname).value = hiddenVal;
	window.setTimeout(function t() {
				document.getElementById(divname).style.display = 'none';
			}, 100);
	 if(!/Port/.test(divname)){
	   		inputValueThisTime();	
	 }
	 if(typeof fillLocaleText != 'undefined' && fillLocaleText instanceof Function){fillLocaleText();}
     if("sailingLoadingPort" == objtxtname){
     	$("#advertthing3Div span:eq(0)").text("ETD " + $("#"+objtxtname).val().substring($("#"+objtxtname).val().indexOf("(")+1,$("#"+objtxtname).val().indexOf(")")));
     }else if("sailingUnloadingPort" == objtxtname){
     	$("#advertthing4Div span:eq(0)").text("ETA " + $("#"+objtxtname).val().substring($("#"+objtxtname).val().indexOf("(")+1,$("#"+objtxtname).val().indexOf(")")));
     }
	 checkPortAndPlace();
}


function writeContent(indexVal) {
	var htmlDiv = '';
	document.getElementById(divname).innerHTML = '';
	htmlDiv += '<span style="font-size:9pt; font-weight:bold;">';
	if (areaarr.length != 0) {
		htmlDiv += '<center style="color:#666; font-size:9pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' + keyword + '</center>';
	} else {
		if(stype == 1){
			hideSelect();
			return;
		}
		//htmlDiv += '<center style="color:#666; font-size:9pt;">&nbsp;&nbsp;&nbsp;Sorry, can not found ' + keyword + '</center>';
	}
	if(portType == 0){
		htmlDiv +='<div style="text-align:left; background-color:lightblue; color:white;">　'+placeLang['os.work.seaPort']+'</div>';
	}else{
		htmlDiv +='<div style="text-align:left; background-color:lightblue; color:white;">　'+placeLang['os.work.airPort']+'</div>';
	}
	if(areaarr.length==0){
		htmlDiv += '<center style="color:#666; font-size:9pt;">&nbsp;&nbsp;&nbsp;Sorry, can not found ' + keyword + '</center>';
	}
	//htmlDiv += '<hr style="BORDER-BOTTOM-STYLE: dotted; BORDER-LEFT-STYLE: dotted; BORDER-RIGHT-STYLE: dotted; BORDER-TOP-STYLE: dotted;color:#AAAAAA;background:#fff;margin:0px auto 11px;width:99%" size="1">';
	var i = 0
	for (; i < areaarr.length; i++) {
		htmlDiv += '<div style="text-align: left" id="' + i + '" hiddenVal="' + areaarr[i][1];
	    if( areaarr[i][0].indexOf("|")!=-1 ){
	    	 var replaceValArr = areaarr[i][0].split("|");
	    	 if( replaceValArr.length==3 ){
	    	     var replaceVal = replaceValArr[2];
	    	     var htmlCode =  '" textVal="' +replaceValArr[0]+'|'+replaceValArr[1]+'|' ;
	    	     var trueVal = '';
	    	     if( replaceVal==1){
	    	        trueVal='↑↑';
	    	        htmlCode +='↑↑"';
	    	     }else if( replaceVal==2){
	    	         trueVal='↑';
	    	     	 htmlCode +='↑"';
	    	     }else if( replaceVal==3 ){
	    	         trueVal='↓';
	    	     	 htmlCode +='↓"';
	    	     }else if( replaceVal==4 ){
	    	         trueVal='↓↓';
	    	     	 htmlCode +='↓↓"';
	    	     }
	    	     htmlDiv +=htmlCode;
	    	     areaarr[i][0] = replaceValArr[0]+"|"+replaceValArr[1]+"|"+trueVal;
	    	 }else{
	    	 	htmlDiv += '" textVal="' + areaarr[i][0]+'"';;
	    	 }
	    }else{
	    	htmlDiv += '" textVal="' + areaarr[i][0]+'"';
	    }
	 
		htmlDiv += ' onMouseOver="javascript:suggestOver(this);"';
		htmlDiv += ' onmouseout="javascript:suggestOut(this);"';
		if(stype != 1){
			htmlDiv += ' onclick="javascript:setSearch(\''
					+ areaarr[i][1].toString() + '\',\'' + areaarr[i][0].toString()
					+ '\');"';
		}
		if (divIdValue == i) {
			textVal = areaarr[i][0];
			hiddenVal = areaarr[i][1];
			htmlDiv += ' class="suggest_link_over" ';
		}
		
		htmlDiv += ' class="suggest_link"><font color="#0055AA"  >'
				+  areaarr[i][0].toString() + '</font></div>';
	}
	
if(sign=="yes"){
	htmlDiv +='<div style="text-align:center;">......</div>';
}
//---------------------------------------------
	htmlDiv += '</span>';
	$('#'+divname).html(htmlDiv);
	document.getElementById(divname).onmouseover = function (){
		isShowDivVal = true;
	}
	document.getElementById(divname).onmouseout = function (){
		isShowDivVal = false;
	}
	markSelect();
	//document.getElementById(divname).style.display = 'block';
}
function gotoPage(indexVal) {    	
	currentNum = indexVal;
	sendAndRepectData();
}


function moveMouseUp() {
	if (divIdValue == 0) {
		divIdValue = areaarr.length - 1;
		if( document.getElementById(divIdValue) != 'undefined'){
		   document.getElementById("0").className = 'suggest_link';
			document.getElementById(divIdValue).className = 'suggest_link_over';
		}
	} else {
		divIdValue = divIdValue - 1;
		if( document.getElementById(divIdValue) != 'undefined'){
			document.getElementById(divIdValue).className = 'suggest_link_over';
			document.getElementById(divIdValue + 1).className = 'suggest_link';
		}
	}
	textVal = document.getElementById(divIdValue).getAttribute("textVal");
	hiddenVal = document.getElementById(divIdValue).getAttribute("hiddenVal");

}

function moveMouseDown() {
	if (divIdValue < (areaarr.length - 1)) {
		divIdValue = divIdValue + 1;
		if( document.getElementById(divIdValue) != 'undefined'){
			document.getElementById(divIdValue).className = 'suggest_link_over';
			document.getElementById(divIdValue - 1).className = 'suggest_link';
		}
	} else if (divIdValue == (areaarr.length - 1)) {
		divIdValue = 0;
		if( document.getElementById(divIdValue) != 'undefined'){
			document.getElementById(areaarr.length - 1).className = 'suggest_link';
			document.getElementById(divIdValue).className = 'suggest_link_over';
		}
	}
	if( document.getElementById(divIdValue) != 'undefined'){
		textVal = document.getElementById(divIdValue).getAttribute("textVal");
		hiddenVal = document.getElementById(divIdValue).getAttribute("hiddenVal");
	}

}

function keyDown(e) {
	var theEvent = window.event || e;
	key = theEvent.keyCode || theEvent.which;

	if (key == 38) {
		moveMouseUp();
	}
	if (key == 40) {
		moveMouseDown();
	}
	if (key == 37 ) {		
     	if( currentNum > 0 && currentNum < count  ){
     	    currentNum = currentNum - 1;     
		}else{
			currentNum = count -1 ; 
		}
		 sendAndRepectData();
	}		
	if (key == 39 ) {
	   if(  currentNum > 0 && currentNum < count ){
	   		currentNum = currentNum + 1;
	   }		 
	  sendAndRepectData();		
	}
	if (key == 13) {	    
		if(stype == 1){
			return;
		}
		if( document.getElementById(divname)==null || document.getElementById(divname).style.display == 'none'){
			return;
		}
		textVal = document.getElementById(divIdValue).getAttribute("textVal");
		hiddenVal = document.getElementById(divIdValue).getAttribute("hiddenVal");
		if( textVal.indexOf("|")!=-1){
		   textVal = textVal.split("|")[0];
		}
		document.getElementById(objtxtname).value = textVal;		
		document.getElementById(objvalname).value = hiddenVal;
		
		document.getElementById(divname).innerHTML = '';
		window.setTimeout(function t() {
					document.getElementById(divname).style.display = 'none';
				}, 100);
	   if(!/Port/.test(divname)){
	   		
	   }
	   if(typeof fillLocaleText != 'undefined' && fillLocaleText instanceof Function){fillLocaleText();}
	   //checkPortAndPlace();
	} else {
		return;
	}
}
function closeDiv(){
	document.getElementById(divname).innerHTML = '';
	document.getElementById(divname).style.display = 'none';
}

function showDiv(obj, divName, width){  
    var divName_Tmp ="#"+divName;
    var oRect = obj.getBoundingClientRect();   
    var top_Tmp =  oRect.left;
    var height_Tmp =  oRect.top;
    
	$(divName_Tmp).css({
		"position": "absolute",
		"top":  height_Tmp +20,
		"left": top_Tmp,
		"width": width
	});
}

function markSelect(){
 	var moreObj =  $('#'+divname);
    moreObj.show();
}
function hideSelect(){
 	var moreObj =  $('#'+divname);
    moreObj.hide();
}

document.onclick = function (){
	if(!isShowDivVal && divname != null && divname != ""){
		document.getElementById(divname).style.display = "none";
		divname = null;
		isShowDivVal = false;
	}
}
